/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package anttasks.file;

import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

/**
 *
 * @author Hue
 */
public class ViewJar extends Task {
    private String file;
    private String filter;
    
    public void setFile(String file) {
        this.file = file;
    }
    
    public void setFilter(String filter) {
        this.filter = filter;
    }

    @Override
    public void execute() throws BuildException {
        print();
    }
    
    protected void print() {
        try {
            JarFile jar = new JarFile(file);
            Enumeration<JarEntry> e = jar.entries();
            StringBuilder sb = new StringBuilder();
            while(e.hasMoreElements()) {
                JarEntry entry = e.nextElement();
                if(filter == null || filter.isEmpty()) {
                    sb.append("    ").append(entry.getName()).append("\r\n");
                }
                else if(entry.getName().toLowerCase().indexOf(filter.toLowerCase()) != -1) {
                    sb.append("    ").append(entry.getName()).append("\r\n");
                }
            }
            if(sb.length() > 0) {
                System.out.println("- " + file + " [");
                System.out.print(sb);
                System.out.println("]");
            }
        }
        catch(Exception ex) {
            throw new BuildException(ex);
        }
    } 
}
